package com.example.daxiguaforharmony.game.core.component;

import com.example.daxiguaforharmony.game.core.base.DisplayObject;
import com.example.daxiguaforharmony.game.core.base.ITouchCallback;
import com.example.daxiguaforharmony.game.core.base.ITouchHandler;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

public class Button implements ITouchHandler {
    private ITouchCallback _touchCallback = null;
    private DisplayObject _displayObject = null;

    public Button(DisplayObject displayObject) {
        this._displayObject = displayObject;
    }

    public void setTouchCallback(ITouchCallback touchCallback) {
        this._touchCallback = touchCallback;
    }

    @Override
    public boolean onTouchEvent(TouchEvent touchEvent) {
        if (this._displayObject == null || this._displayObject.visible == false) return false;
        int action = touchEvent.getAction();
        MmiPoint point = touchEvent.getPointerPosition(0);
        float halfWidth = (this._displayObject.width * this._displayObject.scaleX) / 2f;
        float halfHeight = (this._displayObject.height * this._displayObject.scaleY) / 2f;
        float left = this._displayObject.x - halfWidth;
        float top = this._displayObject.y - halfHeight;
        float right = this._displayObject.x + halfWidth;
        float bottom = this._displayObject.y + halfHeight;
        if (point.getX() >= left && point.getY() >= top && point.getX() <= right && point.getY() <= bottom) {
            if (this._touchCallback != null) {
                this._touchCallback.onTouchEvent(touchEvent);
            }
            return true;
        }
        return false;
    }
}
